import os

class ImageBrowser:
    
    def toggleImageMark(self):
        self.marked_files[self.pos] = not self.marked_files[self.pos]
        
    def markImage(self):
        self.marked_files[self.pos] = True

    def unmarkImage(self):
        self.marked_files[self.pos] = False

    def viewImage(self):
        self.viewed_files[self.pos] = True

    def isMarked(self):
        return self.marked_files[self.pos]
    
    def isViewed(self):
        return self.viewed_files[self.pos]
    
    def rewind(self):
        self.pos = -1
        
    def getImgByIncrement(self, increment, default):
        if self.pos+increment >= len(self.all_files) or self.pos+increment < 0:
            return default
        file = self.all_files[self.pos+increment]
        self.pos = self.pos + increment
        if self.pos >= len(self.all_files) or self.pos < 0:
            return default
        return self.pos
    
    def getNextImg(self):
        return self.all_files[self.getImgByIncrement(1, len(self.all_files)-1)]
    
    def getPreviousImg(self):
        return self.all_files[self.getImgByIncrement(-1, 0)]
        
    def printMarked(self):
        for i in range(0, len(self.all_files)):
            if self.marked_files[i]:
                print "+" + self.all_files[i]
            elif self.viewed_files[i]:
                print "-" + self.all_files[i]

    def _is_valid_extension(self, ext):
        allowed_extensions = ['.jpg', '.JPG', '.gif', '.GIF']
        return ext in allowed_extensions
    
    def __init__(self, path):
        self.pos = -1
        self.marked_files = []
        self.all_files = []
        # find files
        for root, dirs, files in os.walk(path):
            self.all_files = self.all_files + \
                            [os.path.join(root, name) for name in files \
                                                     if self._is_valid_extension(name[len(name)-4:])]
        self.all_files.sort();
        self.marked_files = [False] * len(self.all_files)
        self.viewed_files = [False] * len(self.all_files)
    
    def getCurrentImg(self):
        return self.all_files[self.pos]
    
    def hasNextImg(self):
        return self.all_files and self.pos < len(self.all_files)-1
    
    def hasPreviousImg(self):
        return self.all_files and self.pos > 0
    
if __name__ == '__main__':
    #print sys.argv[1]
    #tmp = pornoganizer(sys.argv[1])
    tmp = pornoganizer('/home/girino/Desktop/old')
    while tmp.hasNextImg():
        print "1", tmp.getNextImg(), tmp.hasNextImg(), tmp.hasPreviousImg()
    while tmp.hasPreviousImg():
        print "2", tmp.getPreviousImg(), tmp.hasNextImg(), tmp.hasPreviousImg()
